import { List } from "../../../fl/controls/List";
import { IFrUI } from "../core/IFrUI";
import { InvalidationType } from "../../../fl/core/InvalidationType";
import { ICellRenderer } from "../../../fl/controls/listClasses/ICellRenderer";
import { UICompomentPool } from "../utils/UICompomentPool";
import { IFrUIContainer } from "../core/IFrUIContainer";
import { IDispose } from "../../gengine/core/IDispose";
import { ListData } from "../../../fl/controls/listClasses/ListData";
import { UIComponent } from "../../../fl/core/UIComponent";
import { ObjEventListerTool } from "../utils/ObjEventListerTool";
type int = number;
//class GList
    
    export  class GList extends List implements IFrUI
    {
        constructor()
        {
            
            super();this._styleName = this.CLASSNAME;
            return;
        }

        public set verticalGap(arg1: number)
        {
            if (this._verticalGap == arg1) 
            {
                return;
            }
            this._verticalGap = arg1;
            invalidate();
            drawNow();
            return;
        }

        public get verticalGap(): number
        {
            return this._verticalGap;
        }

        protected final override configUI(): void
        {
            super.configUI();
            this.createChildren();
            return;
        }

        public get styleName(): string
        {
            return this._styleName;
        }

        public set styleName(arg1: string)
        {
            if (this._styleName != arg1) 
            {
                this._styleName = arg1;
                invalidate(InvalidationType.STYLES);
                this._isStyleChange = true;
            }
            return;
        }

        public getCellRenderer(arg1: Object): ICellRenderer
        {
            var loc1=null;
            if (typeof arg1 === "class") 
            {
                loc1 = UICompomentPool.getUICompoment(arg1 as Class) as Object /* flash.display.DisplayObject */;
                if (typeof loc1 === "ifruicontainer") 
                {
                    (loc1 as IFrUIContainer).createDisposedChildren();
                }
                return loc1 as ICellRenderer;
            }
            return getDisplayObjectInstance(arg1) as ICellRenderer;
        }

        public disposeRender(): void
        {
            var loc1=null;
            if (_dataProvider) 
            {
                _dataProvider.removeAll();
            }
            while (activeCellRenderers.length > 0) 
            {
                loc1 = activeCellRenderers.pop();
                this.removeRenderEventListener(loc1 as Object /* flash.display.Sprite */);
                if (typeof loc1 === "idispose") 
                {
                    (loc1 as IDispose).dispose();
                    continue;
                }
                list.removeChild(loc1 as Object /* flash.display.DisplayObject */);
            }
            while (availableCellRenderers.length > 0) 
            {
                loc1 = availableCellRenderers.pop();
                this.removeRenderEventListener(loc1 as Object /* flash.display.Sprite */);
                if (typeof loc1 === "idispose") 
                {
                    (loc1 as IDispose).dispose();
                    continue;
                }
                if (!(loc1 as Object /* flash.display.DisplayObject */).parent) 
                {
                    continue;
                }
                (loc1 as Object /* flash.display.DisplayObject */).parent.removeChild(loc1 as Object /* flash.display.DisplayObject */);
            }
            return;
        }

        protected removeRenderEventListener(arg1: Object /* flash.display.Sprite */): void
        {
            if (!arg1) 
            {
                return;
            }
            arg1.removeEventListener(flash.events.MouseEvent.CLICK, handleCellRendererClick, false);
            arg1.removeEventListener(flash.events.MouseEvent.ROLL_OVER, handleCellRendererMouseEvent, false);
            arg1.removeEventListener(flash.events.MouseEvent.ROLL_OUT, handleCellRendererMouseEvent, false);
            arg1.removeEventListener(flash.events.Event.CHANGE, handleCellRendererChange, false);
            arg1.doubleClickEnabled = false;
            arg1.removeEventListener(flash.events.MouseEvent.DOUBLE_CLICK, handleCellRendererDoubleClick, false);
            return;
        }

        protected final override draw(): void
        {
            if (isInvalid(InvalidationType.STYLES)) 
            {
                this.updateStyle();
            }
            if (isInvalid(InvalidationType.DATA)) 
            {
                this.updateDate();
            }
            if (isInvalid(InvalidationType.SIZE)) 
            {
                this.updateSize();
            }
            if (isInvalid(InvalidationType.SIZE, InvalidationType.SELECTED, InvalidationType.DATA)) 
            {
                this.updateDisplayList();
            }
            var loc1=(rowHeight * length + this.verticalGap * length - 1);
            var loc2=!(contentHeight == loc1);
            contentHeight = loc1;
            if (isInvalid(InvalidationType.STYLES)) 
            {
                setStyles();
                drawBackground();
                if (contentPadding != getStyleValue("contentPadding")) 
                {
                    invalidate(InvalidationType.SIZE, false);
                }
                if (_cellRenderer != getStyleValue("cellRenderer")) 
                {
                    _invalidateList();
                    _cellRenderer = getStyleValue("cellRenderer");
                }
            }
            if (isInvalid(InvalidationType.SIZE, InvalidationType.STATE) || loc2) 
            {
                drawLayout();
            }
            if (isInvalid(InvalidationType.RENDERER_STYLES)) 
            {
                updateRendererStyles();
            }
            if (isInvalid(InvalidationType.STYLES, InvalidationType.SIZE, InvalidationType.DATA, InvalidationType.SCROLL, InvalidationType.SELECTED)) 
            {
                this.drawList();
            }
            updateChildren();
            validate();
            return;
        }

        protected /* override */ drawList(): void
        {
            var loc5=0;
            var loc6=null;
            var loc7=null;
            var loc10=false;
            var loc11=null;
            var loc12=null;
            var loc13=null;
            var loc14=null;
            var loc1=rowHeight + this.verticalGap;
            var loc15;
            listHolder.y = loc15 = contentPadding;
            listHolder.x = loc15;
            var loc2=listHolder.scrollRect;
            loc2.x = _horizontalScrollPosition;
            loc2.y = Math.floor(_verticalScrollPosition) % loc1;
            listHolder.scrollRect = loc2;
            listHolder.cacheAsBitmap = useBitmapScrolling;
            var loc3=Math.floor(_verticalScrollPosition / loc1);
            var loc4=Math.min(length, loc3 + rowCount + 1);
            renderedItems = loc15 = new Map<any, any> /* flash.utils.Dictionary */(true);
            var loc8=loc15;
            loc5 = loc3;
            while (loc5 < loc4) 
            {
                loc8[_dataProvider.getItemAt(loc5)] = true;
                ++loc5;
            }
            var loc9=new Map<any, any> /* flash.utils.Dictionary */(true);
            while (activeCellRenderers.length > 0) 
            {
                loc6 = (loc7 = activeCellRenderers.pop() as ICellRenderer).data;
                if (loc8[loc6] == null || invalidItems[loc6] == true) 
                {
                    availableCellRenderers.push(loc7);
                }
                else 
                {
                    loc9[loc6] = loc7;
                    invalidItems[loc6] = true;
                }
                list.removeChild(loc7 as Object /* flash.display.DisplayObject */);
            }
            invalidItems = new Map<any, any> /* flash.utils.Dictionary */(true);
            loc5 = loc3;
            while (loc5 < loc4) 
            {
                loc10 = false;
                loc6 = _dataProvider.getItemAt(loc5);
                if (loc9[loc6] == null) 
                {
                    if (availableCellRenderers.length > 0) 
                    {
                        loc7 = availableCellRenderers.pop() as ICellRenderer;
                    }
                    else if ((loc13 = (loc7 = this.getCellRenderer(getStyleValue("cellRenderer"))) as Object /* flash.display.Sprite */) != null) 
                    {
                        loc13.addEventListener(flash.events.MouseEvent.CLICK, handleCellRendererClick, false, 0, true);
                        loc13.addEventListener(flash.events.MouseEvent.ROLL_OVER, handleCellRendererMouseEvent, false, 0, true);
                        loc13.addEventListener(flash.events.MouseEvent.ROLL_OUT, handleCellRendererMouseEvent, false, 0, true);
                        loc13.addEventListener(flash.events.Event.CHANGE, handleCellRendererChange, false, 0, true);
                        loc13.doubleClickEnabled = true;
                        loc13.addEventListener(flash.events.MouseEvent.DOUBLE_CLICK, handleCellRendererDoubleClick, false, 0, true);
                        if (loc13["setStyle"] != null) 
                        {
                            loc15 = 0;
                            var loc16=rendererStyles;
                            for (loc14 in loc16) 
                            {
                                var loc17;
                                (loc17 = loc13)["setStyle"](loc14, rendererStyles[loc14]);
                            }
                        }
                    }
                }
                else 
                {
                    loc10 = true;
                    loc7 = loc9[loc6];
                    delete loc9[loc6];
                }
                list.addChild(loc7 as Object /* flash.display.Sprite */);
                activeCellRenderers.push(loc7);
                loc7.x = 0;
                loc7.y = loc1 * (loc5 - loc3);
                loc7.setSize(availableWidth + _maxHorizontalScrollPosition, rowHeight);
                loc11 = this.itemToLabel(loc6);
                loc12 = null;
                if (_iconFunction == null) 
                {
                    if (_iconField != null) 
                    {
                        if (loc6.hasOwnProperty(_iconField)) 
                        {
                            loc12 = loc6[_iconField];
                        }
                    }
                }
                else 
                {
                    loc12 = _iconFunction(loc6);
                }
                if (!loc10) 
                {
                    loc7.data = loc6;
                }
                loc7.listData = new ListData(loc11, loc12, this, loc5, loc5, 0);
                loc7.selected = !(_selectedIndices.indexOf(loc5) == -1);
                if (typeof loc7 === "uicomponent") 
                {
                    (loc7 as UIComponent).drawNow();
                }
                ++loc5;
            }
            return;
        }

        public /* override */ itemToLabel(arg1: Object): string
        {
            if (_labelFunction != null) 
            {
                return String(_labelFunction(arg1));
            }
            if (_labelField == null || arg1 == null) 
            {
                return "";
            }
            if (arg1.hasOwnProperty(_labelField)) 
            {
                return arg1[_labelField] == null ? "" : string(arg1[_labelField]);
            }
            return "";
        }

        protected createChildren(): void
        {
            return;
        }

        protected updateStyle(): void
        {
            return;
        }

        protected updateSize(): void
        {
            return;
        }

        protected updateDate(): void
        {
            return;
        }

        protected updateDisplayList(): void
        {
            return;
        }

        public configEventListener(arg1: string, arg2: Function, arg3: Boolean=false, arg4: int=0, arg5: Boolean=false): void
        {
            ObjEventListerTool.addObjEvent(this, arg1, arg2, arg3);
            addEventListener(arg1, arg2, arg3, arg4, arg5);
            return;
        }

        public /* override */ removeEventListener(arg1: string, arg2: Function, arg3: Boolean=false): void
        {
            ObjEventListerTool.removeObjEvent(this, arg1, arg2, arg3);
            super.removeEventListener(arg1, arg2, arg3);
            return;
        }

        public dispose(arg1: Boolean=true): void
        {
            this.disposeRender();
            renderedItems = new Map<any, any> /* flash.utils.Dictionary */(true);
            invalidItems = new Map<any, any> /* flash.utils.Dictionary */(true);
            _horizontalScrollPosition = 0;
            _verticalScrollPosition = 0;
            ObjEventListerTool.delObjEvent(this);
            if (this.parent) 
            {
                this.parent.removeChild(this);
            }
            if (arg1) 
            {
                UICompomentPool.disposeUICompoment(this);
            }
            return;
        }

        public /* const */CLASSNAME: string="List";

        private /* var */_verticalGap: number=0;

        private /* var */_styleName: string;

        private /* var */_isStyleChange: Boolean=false;
    }
